/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain an copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jzxiang.pickerview.sample.component;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Picker;
import ohos.app.Context;

import com.jzxiang.pickerview.sample.ResourceTable;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 自定义DatePicker
 *
 * @author wjt
 * @since 2021-04-13
 */
public class DatePickerView extends DirectionalLayout implements Picker.ValueChangedListener {
    /**
     * 数据改变接口实例
     */
    private PickerValueChange pickerValueChange;

    private final int num2 = 2;
    private final int num4 = 4;
    private final int num6 = 6;
    private final int num9 = 9;
    private final int num10 = 10;
    private final int num11 = 11;
    private final int num28 = 28;
    private final int num29 = 29;
    private final int num30 = 30;
    private final int num31 = 31;
    private final int num100 = 100;
    private final int num400 = 400;
    private final int num1900 = 2021;
    private final int num2101 = 2031;
    private final int num13 = 13;
    private final int num32 = 32;
    private final String nian = "年";
    private final String yue = "月";
    private final String ri = "日";
    private Picker picker;
    private Picker picker2;
    private Picker picker3;
    private List<String> yearsWords = new ArrayList<>();
    private List<Integer> years = new ArrayList<Integer>();
    private List<String> mounths = new ArrayList<>();
    private List<String> days = new ArrayList<>();
    private Picker.Formatter yearFormatter;
    private Picker.Formatter mounthsFormatter;
    private Picker.Formatter daysFormatter;
    private String selectYears = "";
    private int selectYears2;
    private String selectMouths = "";
    private String selectDays = "";

    /**
     * 构造
     *
     * @param context 上下文
     */
    public DatePickerView(Context context) {
        super(context);
        initView(context);
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrSet attrset
     */
    public DatePickerView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView(context);
    }

    /**
     * 初始化
     *
     * @param context1
     */
    private void initView(Context context1) {
        Component component1 = LayoutScatter.getInstance(context1)
                .parse(ResourceTable.Layout_layout_date_picker_view_ex, this, false);
        initData();
        initLayout(component1);
        setOrientation(VERTICAL);
        addComponent(component1);
    }

    private void initData() {
        yearsWords.clear();
        years.clear();
        for (int ii = num1900; ii < num2101; ii++) {
            yearsWords.add(ii + nian);
            years.add(ii);
        }
        mounths.clear();
        for (int ii = 1; ii < num13; ii++) {
            mounths.add(ii + yue);
        }
        days.clear();
        for (int ii = 1; ii < num32; ii++) {
            days.add(ii + ri);
        }
    }

    private void initLayout(Component component1) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

        picker = (Picker) component1.findComponentById(ResourceTable.Id_picker);
        picker2 = (Picker) component1.findComponentById(ResourceTable.Id_picker2);
        picker3 = (Picker) component1.findComponentById(ResourceTable.Id_picker3);
        picker3.setWheelModeEnabled(true);
        picker.setValueChangedListener(this);
        picker2.setValueChangedListener(this);
        picker3.setValueChangedListener(this);
        int index = yearsWords.indexOf(currentYear + nian);
        selectYears = currentYear + nian;
        picker.setValue(index);
        picker.setMinValue(0);
        picker.setMaxValue(yearsWords.size() - 1);
        yearFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return yearsWords.get(i);
            }
        };
        picker.setFormatter(yearFormatter);
        int index2 = mounths.indexOf(currentMonth + yue);
        selectMouths = currentMonth + yue;
        picker2.setValue(index2);
        picker2.setMinValue(0);
        picker2.setMaxValue(mounths.size() - 1);
        mounthsFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return mounths.get(i);
            }
        };
        picker2.setFormatter(mounthsFormatter);

        int index3 = days.indexOf(currentDay + ri);
        selectDays = currentDay + ri;
        picker3.setValue(index3);
        picker3.setMinValue(0);
        picker3.setMaxValue(mounths.size() - 1);
        daysFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return days.get(i);
            }
        };
        picker3.setFormatter(daysFormatter);

        picker.setValue(0);
    }

    /**
     * 获得当前月份的天数
     *
     * @param year 年份
     * @param month 月份
     * @return 获取当前年月所对应日期的最大天数
     */
    public int getMaxDay(final int year, int month) {
        switch (month) {
            case num2:
                int flagF = year % num400;
                int flagS = year % num4;
                int flagT = year % num100;
                return flagF == 0 || (flagS == 0 && flagT != 0) ? num29 : num28;
            case num4:
            case num6:
            case num9:
            case num11:
                return num30;
            default:
                return num31;
        }
    }

    @Override
    public void onValueChanged(Picker view, int i, int change) {
        switch (view.getId()) {
            case ResourceTable.Id_picker:
                selectYears = yearsWords.get(change);
                selectYears2 = years.get(change);
                if (pickerValueChange != null) {
                    pickerValueChange.valueChange(selectYears, selectMouths, selectDays);
                }
                break;
            case ResourceTable.Id_picker2:
                selectMouths = mounths.get(change);
                int daynums = getMaxDay(selectYears2, change + 1);
                days.clear();
                for (int jj = 1; jj < daynums + 1; jj++) {
                    days.add(jj + ri);
                }
                picker3.setMinValue(0);
                picker3.setMaxValue(days.size() - 1);
                picker3.setFormatter(null);
                picker3.setFormatter(daysFormatter);
                picker3.setValue(0);
                if (pickerValueChange != null) {
                    pickerValueChange.valueChange(selectYears, selectMouths, selectDays);
                }
                break;
            case ResourceTable.Id_picker3:
                selectDays = days.get(change);
                if (pickerValueChange != null) {
                    pickerValueChange.valueChange(selectYears, selectMouths, selectDays);
                }
                break;
            default:break;
        }
    }

    public void setValuChange(PickerValueChange valueChange) {
        this.pickerValueChange = valueChange;
    }
}
